// ------------------------------------------------------------------------------------------
// Licensed by Interprise Solutions.
// http://www.InterpriseSolutions.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace InterpriseSuiteEcommerceControls.mobile
{
    public class OrderHistoryControl : CompositeControl
    {
        #region Variable Declaration

        private Label _lblPages = new Label();
        private TextBox _txtPages = new TextBox();
        private HyperLink _lnkReset = new HyperLink();
        private Panel _pnlPager = new Panel();        
        private Panel _pnlMain = new Panel();
        private Panel _pnlContent = new Panel();
        private Panel _pnlPagingHeader = new Panel();
        private Panel _pnlPagingFooter = new Panel();
        private const int DEFAULT_PAGE_COUNT = 25;

        #endregion

        public string PageCaption
        {
            get { return _lblPages.Text; }
            set { _lblPages.Text = value; }
        }

        public string ResetCaption
        {
            get { return _lnkReset.Text; }
            set { _lnkReset.Text = value; }
        }

        public int PageCount
        {
            get
            {
                int pageCount = DEFAULT_PAGE_COUNT;
                if(!int.TryParse(_txtPages.Text, out pageCount))
                {
                    _txtPages.Text = DEFAULT_PAGE_COUNT.ToString();                    
                }

                return pageCount;
            }
            set
            {
                _txtPages.Text = value.ToString();
            }
        }

        public OrderHistoryControl()
        {
            _pnlMain.ID = "Main";
            _pnlContent.ID = "Content";
            _pnlPager.ID = "Pager";
            _pnlPagingHeader.ID = "PagingHeader";
            _pnlPagingFooter.ID = "PagingFooter";

            _txtPages.ID = "Pages";
            _txtPages.MaxLength = 4;
            _txtPages.Columns = 5;
            _lnkReset.ID = "Reset";
            _lnkReset.NavigateUrl = "javascript:void(0);";

            AssignStyles();
        }

        private void AssignStyles()
        {
            _pnlMain.CssClass = "OrderHistoryMain";
            _pnlPagingHeader.CssClass = "OrderHistoryPagingHeader";
            _pnlPagingFooter.CssClass = "OrderHistoryPagingFooter";
            _pnlContent.Width = Unit.Percentage(100);
        }

        protected override void CreateChildControls()
        {
            _pnlMain.Controls.Add(_pnlPagingHeader);
            _pnlMain.Controls.Add(_pnlContent);
            _pnlMain.Controls.Add(_pnlPagingFooter);

            _pnlPager.Controls.Add(_lblPages);
            _pnlPager.Controls.Add(new LiteralControl("&nbsp;"));
            _pnlPager.Controls.Add(_txtPages);
            _pnlPager.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            _pnlPager.Controls.Add(_lnkReset);

            _pnlMain.Controls.Add(_pnlPager);            

            Controls.Add(_pnlMain);

            base.CreateChildControls();
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            if (!DesignMode)
            {
                var script = new StringBuilder();
                script.Append("<script type=\"text/javascript\" >\n");
                //script.Append("Event.observe(window, 'load', \n");
                script.Append("$(document).ready(\n");
                script.Append(" function() { \n");
                script.AppendFormat("     var grdOrderHistory = new ise.Pages.Account.OrderHistoryControl('{0}');\n", this.ClientID);
                script.Append("     grdOrderHistory.changePage(1);\n");
                script.Append(" }\n");
                script.Append(");\n");
                script.AppendLine();
                script.Append("</script>\n");

                writer.Write(script.ToString());
            }
        }
    }
}

